package com.jsbs.iam.user.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.common.core.utils.RegexUtils;
import com.jsbs.iam.common.core.utils.StringUtils;
import com.jsbs.iam.common.core.utils.poi.ExcelUtil;
import com.jsbs.iam.user.constant.Constants;
import com.jsbs.iam.user.dto.*;
import com.jsbs.iam.user.entity.IamUserInfo;
import com.jsbs.iam.user.entity.IamUserTypeBase;
import com.jsbs.iam.user.entity.IamUserTypeExtendedField;
import com.jsbs.iam.user.feign.IdentFeignInterface;
import com.jsbs.iam.user.param.*;
import com.jsbs.iam.user.service.UserInfoService;
import com.jsbs.iam.user.vo.UserInfoSelectVo;
import com.jsbs.iam.user.vo.UserInfoVo;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author junwc
 * @Date 2022/12/8
 * @Version 1.0
 */
@RestController
@RequestMapping("/iam/user/info")
public class UserInfoController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private IdentFeignInterface identFeignInterface;

    private static final String ADMIN_USER = "admin";
    /**
     * 功能描述：根据名称/用户编码/手机号模糊搜索用户
     * @param request
     * @param userQueryParam
     * @return
     */
    @PostMapping("/queryuser")
    public Result queryForUser(HttpServletRequest request, @RequestBody UserQueryParam userQueryParam) {
        logger.info("Route==>UserInfoController method=queryForUser 查询用户列表入参：{}", JSON.toJSONString(userQueryParam));
        try {
            //前端控制，后端不用管
            if (StringUtils.isEmpty(userQueryParam.getQueryParam())) {
                return Result.failWithMsg("查询条件为空");
            }
            if (userQueryParam.getQueryParam().length() > 30) {
                return Result.failWithMsg("查询条件长度超过30");
            }
            String companyCode = (String) request.getSession().getAttribute("companyCode");
            //暂时先使用伪数据处理companyCode
            companyCode="000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.info("Route==>UserInfoController method=queryForUser 当前登录人归属租户信息异常:{}", JSON.toJSONString(userQueryParam));
                return Result.failWithMsg("归属租户为空");
            }
            userQueryParam.setCompanyCode(companyCode);
            // 根据用户编码/名称/手机号搜索
            PageInfo<UserQueryResp> respList = userInfoService.queryUserByParam(userQueryParam);
            return Result.success(respList);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=queryForUser 查询用户列表异常：", e);
            return handleException(e);
        }
    }
    /**
     * 功能描述：根据名称/用户编码/手机号模糊搜索用户,只查询未开通账户的用户
     * @param request
     * @param userQueryParam
     * @return
     */
    @PostMapping("/queryUserNotOpen")
    public Result queryUserNotOpen(HttpServletRequest request, @RequestBody UserQueryParam userQueryParam) {
        try {
            String companyCode = (String) request.getSession().getAttribute("companyCode");
            //暂时先使用伪数据处理companyCode
            companyCode="000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.info("Route==>UserInfoController method=queryForUser 当前登录人归属租户信息异常:{}", JSON.toJSONString(userQueryParam));
                return Result.failWithMsg("归属租户为空");
            }
            userQueryParam.setCompanyCode(companyCode);
            // 根据用户编码/名称/手机号搜索
            PageInfo<UserQueryResp> respList = userInfoService.queryUserNotOpen(userQueryParam);
            return Result.success(respList);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=queryUserNotOpen 查询用户列表异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：根据名称/用户编码/手机号模糊搜索绑定用户列表 分页
     * @param request
     * @param userQueryParam
     * @return
     */
    @PostMapping("/bindlist")
    public Result queryForBindUser(HttpServletRequest request, @RequestBody UserQueryParam userQueryParam) {
        logger.info("Route==>UserInfoController method=queryForBindUser 查询管理员角色绑定用户列表入参：{}", JSON.toJSONString(userQueryParam));
        try {
            /*String companyCode = (String) request.getSession().getAttribute("companyCode");
            if (StringUtils.isEmpty(companyCode)) {
                logger.info("Route==>UserInfoController method=queryForBindUser 查询管理员角色绑定用户信息异常:{}", JSON.toJSONString(userQueryParam));
                return Result.failWithMsg("归属租户为空");
            }*/
            if (StringUtils.isNotEmpty(userQueryParam.getQueryParam()) && userQueryParam.getQueryParam().length() > 30) {
                return Result.failWithMsg("查询条件长度超过30");
            }
            userQueryParam.initPage();
            // 根据编码/名称/手机号搜索
            PageInfo<UserQueryResp> respList = userInfoService.queryBinadUser(userQueryParam);
            return Result.success(respList);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=queryForBindUser 查询管理员角色绑定用户列表异常：", e);
            return handleException(e);
        }
    }

    /**
     * 获取用户类型扩展字段数据
     * @param userTypeCode
     * @return
     */
    @PostMapping("/getUserTypeExtend")
    public Result getUserTypeExtend(HttpServletRequest httpServletRequest, @RequestParam("userTypeCode") String userTypeCode) {
        logger.info("Route=>UserInfoController method=>getUserTypeExtend 获取用户类型扩展字段数据：{}", userTypeCode);
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = (String) httpServletRequest.getSession().getAttribute("companyCode");
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>getUserTypeExtend 获取用户类型扩展字段数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(userTypeCode)) {
                logger.info("获取用户类型扩展字段数据失败,参数非法{}", userTypeCode);
                return Result.failWithMsg("用户类型为空");
            }
            List<IamUserTypeExtendedField> userTypeExtendedFields = userInfoService.getUserTypeExtend(userTypeCode,companyCode);

            return new Result(Result.SUCCESS, "组织类型扩展字段数据成功", userTypeExtendedFields);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>getUserTypeExtend 获取用户类型扩展字段数据异常，入参：{}，异常：{}", userTypeCode, e);
            return handleException(e);
        }
    }

    /**
     * 新增用户
     * @param userInfoAddDto
     * @return
     */
    @PostMapping("/addUserInfo")
    public Result addUserInfo(HttpServletRequest httpServletRequest, @RequestBody UserInfoAddDto userInfoAddDto) {
        logger.info("Route=>UserInfoController method=>addUserInfo 新增用户：{}", Json.toJsonString(userInfoAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String adminRoleCode = httpServletRequest.getHeader(Constants.ADMIN_ROLE_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            // userCode = "001001";
            // adminRoleCode = "JS0030";
            userName = "wjy";
            logger.info("Route=>UserInfoController method=>addUserInfo 新增用户userCode:{},adminRoleCode:{}", userCode, adminRoleCode);
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>addUserInfo 新增用户，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>UserInfoController method=>addUserInfo 新增用户，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>UserInfoController method=>addUserInfo 新增用户，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (!ADMIN_USER.equals(userCode) && StringUtils.isEmpty(adminRoleCode)) {
                logger.warn("Route=>UserInfoController method=>addUserInfo 新增用户，当前登录人角色信息异常");
                return Result.failWithMsg("登录人角色信息异常");
            }

            userInfoAddDto.setCompanyCode(companyCode);
            userInfoAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(userInfoAddDto.getOrgTypeCode())) {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getOrgTypeCode());
                return Result.fail(Result.FAIL, "组织类型编码不可为空");
            }
            if (StringUtils.isEmpty(userInfoAddDto.getOrgCode())) {
                logger.info("新增用户失败,参数非法{}", Json.toJsonString(userInfoAddDto));
                return Result.fail(Result.FAIL, "组织编码不可为空");
            }else if (!ADMIN_USER.equals(userCode)){//普管
                //判断是否为普管下的组织
                //按照","获取组织编码
                String[] orgCodeStr = userInfoAddDto.getOrgCode().split(",");
                List<String> orgCodes = Arrays.asList(orgCodeStr);
                //查询普管的组织权限
                List<String> orgCodeList = userInfoService.getUserOrgPermission(adminRoleCode, companyCode);
                //判断所选组织是否在权限范围内
                boolean includeFlag = this.includeOrg(orgCodeList, orgCodes);
                if (!includeFlag) {
                    logger.info("新增用户失败,参数非法{}", Json.toJsonString(userInfoAddDto));
                    return Result.fail(Result.FAIL, "所选组织不在当前登录人的权限范围内!!!");
                }
            }
            if (StringUtils.isEmpty(userInfoAddDto.getName())) {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getName());
                return Result.fail(Result.FAIL, "姓名不可为空");
            } else if (userInfoAddDto.getName().length() > 30) {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getName());
                return Result.fail(Result.FAIL, "姓名长度大于30");
            }
            if (userInfoAddDto.getGender() == null) {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getGender());
                return Result.fail(Result.FAIL, "性别不可为空");
            }
            if (StringUtils.isEmpty(userInfoAddDto.getPhone())) {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getPhone());
                return Result.fail(Result.FAIL, "联系号码不可为空");
            } else if (!RegexUtils.isMobileSimple(userInfoAddDto.getPhone())) {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getPhone());
                return Result.fail(Result.FAIL, "联系号码格式不为手机号");
            } else {
                //校验同一组织类型下只能有一个手机号
                int count = userInfoService.verifyPhone(userInfoAddDto.getOrgTypeCode(), userInfoAddDto.getPhone(), companyCode);
                if (count > 0) {
                    logger.info("新增用户失败,参数非法{}", userInfoAddDto.getPhone());
                    return Result.fail(Result.FAIL, "同一组织类型下联系号码唯一");
                }
            }
            if (StringUtils.isNotEmpty(userInfoAddDto.getEmail())) {
                if (userInfoAddDto.getEmail().length() > 30) {
                    logger.info("新增用户失败,参数非法{}", userInfoAddDto.getEmail());
                    return Result.fail(Result.FAIL, "电子邮箱长度大于30");
                } else if (!RegexUtils.isEmail(userInfoAddDto.getEmail())) {
                    logger.info("新增用户失败,参数非法{}", userInfoAddDto.getPhone());
                    return Result.fail(Result.FAIL, "电子邮箱格式不正确");
                }
            }
            if (userInfoAddDto.getEmployeeStatus()== null) {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getEmployeeStatus());
                return Result.fail(Result.FAIL, "状态不可为空");
            }
            //校验添加用户是否选择用户类型
            if (StringUtils.isNotEmpty(userInfoAddDto.getUserTypeCode())) {
                Result result = this.verifyUserType(userInfoAddDto);
                if (!result.isSuccess()) {
                    return result;
                }

//                //查询用户类型扩展字段是否可以全部为空
//                int count = userInfoService.verifyAllNull(userInfoAddDto.getUserTypeCode(), companyCode);
//                //不能全部为空
//                if (count > 0) {
//                    //新增用户数据校验扩展字段规则
//                    if (org.apache.commons.collections.CollectionUtils.isNotEmpty(userInfoAddDto.getExtendData())) {
//                        Result msg = verifyExtendData(userInfoAddDto);
//                        if (msg.getCode() == 9) {
//                            logger.info("新增用户失败,参数非法{}:::" + msg.getMsg(), userInfoAddDto.getExtendData());
//                            return Result.fail(Result.FAIL, msg.getMsg());
//                        }
//                    } else {
//                        logger.info("新增用户失败,参数非法{}", userInfoAddDto.getExtendData());
//                        return Result.fail(Result.FAIL, "扩展字段数据不可为空");
//                    }
//                }
            } else {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getUserTypeCode());
                return Result.fail(Result.FAIL, "用户类型不可为空");
            }
            //新增用户
            userInfoService.addUserInfo(userInfoAddDto);
            return new Result(Result.SUCCESS, "新增用户成功");
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>addUserInfo 新增用户异常，入参：{}，异常：{}", Json.toJsonString(userInfoAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 校验用户类型扩展字段
     * @param userInfoAddDto
     * @return
     */
    private Result verifyUserType(UserInfoAddDto userInfoAddDto) {
        //查询用户类型扩展字段是否可以全部为空
        int count = userInfoService.verifyAllNull(userInfoAddDto.getUserTypeCode(), userInfoAddDto.getCompanyCode());
        //不能全部为空
        if (count > 0) {
            //新增用户数据校验扩展字段规则
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(userInfoAddDto.getExtendData())) {
                Result msg = verifyExtendData(userInfoAddDto);
                if (msg.getCode() == 9) {
                    logger.info("新增用户失败,参数非法{}:::" + msg.getMsg(), userInfoAddDto.getExtendData());
                    return Result.fail(Result.FAIL, msg.getMsg());
                }
            } else {
                logger.info("新增用户失败,参数非法{}", userInfoAddDto.getExtendData());
                return Result.fail(Result.FAIL, "扩展字段数据不可为空");
            }
        }
        return Result.successWithMsg("校验完成");
    }

    /**
     * 判断orgCodes是否在orgCodeList范围内
     * @param orgCodeList  大集合
     * @param orgCodes  子集合
     * @return
     */
    private boolean includeOrg(List<String> orgCodeList, List<String> orgCodes) {
        boolean anyMatch = false;
        for (String orgCode : orgCodes) {
            //判断子集合中的元素是否在大集合中
            anyMatch = orgCodeList.stream().anyMatch(f -> f.equals(orgCode));
            if (!anyMatch) {
                return anyMatch;
            }
        }
        return anyMatch;
    }

    /**
     * 用户数据校验扩展字段规则
     * @param userInfoAddDto
     * @return
     */
    private Result verifyExtendData(UserInfoAddDto userInfoAddDto) {
        String msgStr = "";
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(userInfoAddDto.getExtendData())) {
            for (Map<String, String> extendDatum : userInfoAddDto.getExtendData()) {
                if (MapUtils.isNotEmpty(extendDatum)) {
                    Set<String> keys = extendDatum.keySet();
                    for (String fieldId : keys) {
                        //查询对应字段详细数据
                        IamUserTypeExtendedField extendedField = userInfoService.getExtendInfo(fieldId,userInfoAddDto.getCompanyCode());
                        //取出填写的数据
                        String fieldData = extendDatum.get(fieldId);
                        //判断是否可以为空
                        //允许为空 0:不允许为空  1:允许为空
                        Integer isNull = extendedField.getIsNull();
                        //0:不允许为空
                        if (isNull != null && isNull == 0) {
                            if (StringUtils.isEmpty(fieldData)) {
                                msgStr = extendedField.getFieldName() + "不能为空";
                                return Result.failWithMsg(msgStr);
                            } else {
                                //校验字段类型
                                if (!verifyType(extendedField.getFieldType(), fieldData)) {
                                    msgStr = extendedField.getFieldName() + "不能输入非" + extendedField.getFieldType() + "类型数据";
                                    return Result.failWithMsg(msgStr);
                                }
                                //校验字段长度
                                if (fieldData.length() > extendedField.getFieldLength()) {
                                    msgStr = extendedField.getFieldName() + "输入长度大于" + extendedField.getFieldLength();
                                    return Result.failWithMsg(msgStr);
                                }
                                //校验是否唯一
                                //是否唯一 0:不允许唯一  1:允许唯一
                                Integer isSole = extendedField.getIsSole();
                                if (isSole != null && isSole == 1) {
                                    if (StringUtils.isEmpty(userInfoAddDto.getUserCode())) {
                                        //新增
                                        //校验扩展字段唯一性
                                        int count = userInfoService.verifyIsSole(fieldId, fieldData, userInfoAddDto.getCompanyCode());
                                        if (count > 0) {
                                            msgStr = extendedField.getFieldName() + "必须唯一, 请重新输入";
                                            return Result.failWithMsg(msgStr);
                                        }
                                    } else {
                                        //修改
                                        //校验是否修改数据
                                        String fieldInfo = userInfoService.verifyFieldData(fieldId, userInfoAddDto.getUserCode(), userInfoAddDto.getCompanyCode());
                                        if (StringUtils.isNotEmpty(fieldInfo) && !fieldData.equals(fieldInfo)) {
                                            //校验扩展字段唯一性
                                            int count = userInfoService.verifyIsSole(fieldId, fieldData, userInfoAddDto.getCompanyCode());
                                            if (count > 0) {
                                                msgStr = extendedField.getFieldName() + "必须唯一, 请重新输入";
                                                return Result.failWithMsg(msgStr);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return Result.success();
    }

    /**
     * 校验字段类型
     * @param type
     * @param fieldData
     * @return
     */
    public boolean verifyType(String type, String fieldData) {
        if (StringUtils.isNotEmpty(fieldData)) {
            if ("Integer".equals(type)) {
                return isNumeric(fieldData);
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 判断是否为数字
     * @param str
     * @return
     */
    public boolean isNumeric(String str)
    {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() )
        {
            return false;
        }
        return true;
    }

    /**
     * 根据条件查询组织下用户数据
     * @param userInfoSelectDto
     * @return
     */
    @PostMapping("/selectUserInfo")
    public Result selectUserInfo(HttpServletRequest httpServletRequest, @RequestBody UserInfoSelectDto userInfoSelectDto) {
        logger.info("Route=>UserInfoController method=>selectUserInfo 根据条件查询用户数据：{}", Json.toJsonString(userInfoSelectDto));
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>selectUserInfo 根据条件查询用户数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            userInfoSelectDto.setCompanyCode(companyCode);

            if (StringUtils.isEmpty(userInfoSelectDto.getOrgTypeCode())) {
                logger.info("根据条件查询用户数据失败,参数非法{}", userInfoSelectDto.getOrgTypeCode());
                return Result.fail(Result.FAIL, "组织类型编码不可为空");
            }
            //判断是否为只是查询组织类型下的所有人员
            if (userInfoSelectDto.getOrgLevel() == null || userInfoSelectDto.getOrgLevel() != 0) {
                if (StringUtils.isEmpty(userInfoSelectDto.getOrgCode())) {
                    logger.info("根据条件查询用户数据失败,参数非法{}", userInfoSelectDto.getOrgCode());
                    return Result.fail(Result.FAIL, "组织编码不可为空");
                }else {
                    //判断用户组织编码后是否有","相连
                    if (!",".equals(userInfoSelectDto.getOrgCode().substring(userInfoSelectDto.getOrgCode().length() - 1))) {
                        userInfoSelectDto.setOrgCode(userInfoSelectDto.getOrgCode() + ",");
                    }
                }
            }
            if (StringUtils.isNotEmpty(userInfoSelectDto.getEmployeeCode())) {
                if (userInfoSelectDto.getEmployeeCode().length() > 30) {
                    logger.info("根据条件查询用户数据失败,参数非法{}", userInfoSelectDto.getEmployeeCode());
                    return Result.fail(Result.FAIL, "用户编码长度大于30");
                }
            }
            if (StringUtils.isNotEmpty(userInfoSelectDto.getName())) {
                if (userInfoSelectDto.getName().length() > 30) {
                    logger.info("根据条件查询用户数据失败,参数非法{}", userInfoSelectDto.getName());
                    return Result.fail(Result.FAIL, "用户姓名长度大于30");
                }
            }
            if (StringUtils.isNotEmpty(userInfoSelectDto.getPhone())) {
                if (userInfoSelectDto.getPhone().length() > 20) {
                    logger.info("根据条件查询用户数据失败,参数非法{}", userInfoSelectDto.getPhone());
                    return Result.fail(Result.FAIL, "用户联系号码长度大于30");
                }
            }
            //根据条件查询用户数据
            PageInfo userInfo = userInfoService.selectUserInfo(userInfoSelectDto);
            return new Result(Result.SUCCESS, "根据条件查询用户数据成功",userInfo);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>selectUserInfo 根据条件查询用户数据异常，入参：{}，异常：{}", Json.toJsonString(userInfoSelectDto), e);
            return handleException(e);
        }
    }

    /**
     * 批量删除用户数据
     * @param userInfoDeleteDto
     * @return
     */
    @PostMapping("/deleteUserInfo")
    public Result deleteUserInfo(HttpServletRequest httpServletRequest,@RequestBody UserInfoDeleteDto userInfoDeleteDto) {
        logger.info("Route=>UserInfoController method=>deleteUserInfo 批量删除用户数据：{}", Json.toJsonString(userInfoDeleteDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>deleteUserInfo 批量删除用户数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>UserInfoController method=>deleteUserInfo 批量删除用户数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>UserInfoController method=>deleteUserInfo 批量删除用户数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (CollectionUtils.isEmpty(userInfoDeleteDto.getUserCodes())) {
                logger.info("批量删除用户数据,参数非法{}", userInfoDeleteDto.getUserCodes());
                return new Result(Result.FAIL, "请选择数据再进行删除！");
            }
            userInfoDeleteDto.setCompanyCode(companyCode);
            userInfoDeleteDto.setUpdatePerson(userName);
            //批量删除用户数据
            userInfoService.deleteUserInfo(userInfoDeleteDto,httpServletRequest);
            return new Result(Result.SUCCESS, "删除成功");
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>deleteUserInfo 批量删除用户数据异常，入参：{}，异常：{}", Json.toJsonString(userInfoDeleteDto), e);
            return handleException(e);
        }
    }

    /**
     * 查询用户详情数据
     * @param userCode
     * @return
     */
    @PostMapping("/selectUserDetail")
    public Result selectUserDetail(HttpServletRequest httpServletRequest,@RequestParam("userCode") String userCode) {
        logger.info("Route=>UserInfoController method=>selectUserDetail 查询用户详情数据：{}", userCode);
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>selectUserDetail 查询用户详情数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(userCode)) {
                return new Result(Result.FAIL, "请选择用户数据！");
            }
            //查询用户详情数据
            UserInfoVo userInfoVo = userInfoService.selectUserDetail(userCode,companyCode);
            return new Result(Result.SUCCESS, "查询用户详情数据成功", userInfoVo);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>selectUserDetail 查询用户详情数据异常，入参：{}，异常：{}", userCode, e);
            return handleException(e);
        }
    }

    /**
     * 修改用户详情数据
     * @param userInfoAddDto
     * @return
     */
    @PostMapping("/updateUserDetail")
    public Result updateUserDetail(HttpServletRequest httpServletRequest,@RequestBody UserInfoAddDto userInfoAddDto) {
        logger.info("Route=>UserInfoController method=>updateUserDetail 修改用户详情数据：{}", Json.toJsonString(userInfoAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String adminRoleCode = httpServletRequest.getHeader(Constants.ADMIN_ROLE_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            userCode = "admin";
            adminRoleCode = "JS0030";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>updateUserDetail 修改用户详情数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>UserInfoController method=>updateUserDetail 修改用户详情数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>UserInfoController method=>updateUserDetail 修改用户详情数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (!ADMIN_USER.equals(userCode) && StringUtils.isEmpty(adminRoleCode)) {
                logger.warn("Route=>UserInfoController method=>updateUserDetail 修改用户详情数据，当前登录人角色信息异常");
                return Result.failWithMsg("登录人角色信息异常");
            }
            userInfoAddDto.setCompanyCode(companyCode);
            userInfoAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(userInfoAddDto.getUserCode())) {
                logger.info("修改用户详情数据失败,参数非法{}", userInfoAddDto.getUserCode());
                return Result.fail(Result.FAIL, "用户唯一标识用户id不可为空");
            }
            if (StringUtils.isEmpty(userInfoAddDto.getOrgTypeCode())) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getOrgTypeCode());
                return Result.fail(Result.FAIL, "组织类型编码不可为空");
            }
            if (StringUtils.isEmpty(userInfoAddDto.getOrgCode())) {
                logger.info("修改用户失败,参数非法{}", Json.toJsonString(userInfoAddDto));
                return Result.fail(Result.FAIL, "组织编码不可为空");
            }else if (!ADMIN_USER.equals(userCode)) {//普管
                //判断是否为普管下的组织
                //按照","获取组织编码
                String[] orgCodeStr = userInfoAddDto.getOrgCode().split(",");
                List<String> orgCodes = Arrays.asList(orgCodeStr);
                //查询普管的组织权限
                List<String> orgCodeList = userInfoService.getUserOrgPermission(adminRoleCode, companyCode);
                //判断所选组织是否在权限范围内  TODO  wjy  着重测试
                boolean includeFlag = this.includeOrg(orgCodeList, orgCodes);
                if (!includeFlag) {
                    logger.info("新增用户失败,参数非法{}", Json.toJsonString(userInfoAddDto));
                    return Result.fail(Result.FAIL, "所选组织不在当前登录人的权限范围内!!!");
                }
            }
            if (StringUtils.isEmpty(userInfoAddDto.getName())) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getName());
                return Result.fail(Result.FAIL, "姓名不可为空");
            } else if (userInfoAddDto.getName().length() > 30) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getName());
                return Result.fail(Result.FAIL, "姓名长度大于30");
            }
            if (userInfoAddDto.getGender() == null) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getGender());
                return Result.fail(Result.FAIL, "性别不可为空");
            }
            if (StringUtils.isEmpty(userInfoAddDto.getPhone())) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getPhone());
                return Result.fail(Result.FAIL, "联系号码不可为空");
            } else if (!RegexUtils.isMobileSimple(userInfoAddDto.getPhone())) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getPhone());
                return Result.fail(Result.FAIL, "联系号码格式不为手机号");
            } else {
                //校验是否修改手机号
                UserInfoVo userInfoVo = userInfoService.selectUserDetail(userInfoAddDto.getUserCode(), companyCode);
                String phone = userInfoVo.getPhone();
                if (!userInfoAddDto.getPhone().equals(phone)) {
                    //校验同一组织类型下只能有一个手机号
                    int count = userInfoService.verifyPhone(userInfoAddDto.getOrgTypeCode(), userInfoAddDto.getPhone(), companyCode);
                    if (count > 0) {
                        logger.info("修改用户失败,参数非法{}", userInfoAddDto.getPhone());
                        return Result.fail(Result.FAIL, "同一组织类型下联系号码唯一");
                    }
                }
            }
            if (StringUtils.isNotEmpty(userInfoAddDto.getEmail())) {
                if (userInfoAddDto.getEmail().length() > 30) {
                    logger.info("修改用户失败,参数非法{}", userInfoAddDto.getEmail());
                    return Result.fail(Result.FAIL, "电子邮箱长度大于30");
                } else if (!RegexUtils.isEmail(userInfoAddDto.getEmail())) {
                    logger.info("修改用户失败,参数非法{}", userInfoAddDto.getPhone());
                    return Result.fail(Result.FAIL, "电子邮箱格式不正确");
                }
            }
            if (userInfoAddDto.getEmployeeStatus()== null) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getEmployeeStatus());
                return Result.fail(Result.FAIL, "状态不可为空");
            }
            if (StringUtils.isEmpty(userInfoAddDto.getUserTypeCode())) {
                logger.info("修改用户失败,参数非法{}", userInfoAddDto.getEmployeeStatus());
                return Result.fail(Result.FAIL, "用户类型不可为空");
            }else {
                //校验用户的用户类型数据
                String userTypeCode = userInfoService.verifyUserType(userInfoAddDto);
                if (StringUtils.isEmpty(userTypeCode)) {//判断是否为导入数据,导入数据用户类型为空
                    userInfoAddDto.setOperateType("0");//"0":新增当前扩展数据
                } else if (userTypeCode.equals(userInfoAddDto.getUserTypeCode())) {//判断是否修改用户类型数据
                    userInfoAddDto.setOperateType("1");//"1":修改原有扩展数据
                } else {
                    userInfoAddDto.setOperateType("2");//"2":删除原有扩展数据,新增当前扩展数据
                }
                Result result = this.verifyUserType(userInfoAddDto);
                if (!result.isSuccess()) {
                    return result;
                }
            }
            //修改用户数据校验扩展字段规则
//            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(userInfoAddDto.getExtendData())) {
//                Result msg = verifyExtendData(userInfoAddDto);
//                if (msg.getCode() == 9) {
//                    logger.info("修改用户失败,参数非法{}:::" + msg.getMsg(), userInfoAddDto.getExtendData());
//                    return Result.fail(Result.FAIL, msg.getMsg());
//                }
//            }
            //修改用户详情数据
            userInfoService.updateUserDetail(userInfoAddDto);
            return new Result(Result.SUCCESS, "修改用户详情数据成功");
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>updateUserDetail 修改用户详情数据异常，入参：{}，异常：{}", Json.toJsonString(userInfoAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 用户数据导入
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/userInfoImport")
    public Result userInfoImport(HttpServletRequest httpServletRequest,@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        logger.info("Route=>UserInfoController method=>userInfoImport 用户数据导入：{}", file.getName());
        if (Objects.isNull(file)) {
            throw new RuntimeException("所选的导入文件数据为空，请重新导入！");
        }
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String adminRoleCode = httpServletRequest.getHeader(Constants.ADMIN_ROLE_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            userCode = "admin";
            adminRoleCode = "JS0030";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>userInfoImport 用户数据导入，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            if (StringUtils.isEmpty(userCode)) {
                logger.warn("Route=>UserInfoController method=>userInfoImport 用户数据导入，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (StringUtils.isEmpty(userName)) {
                logger.warn("Route=>UserInfoController method=>userInfoImport 用户数据导入，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            if (!ADMIN_USER.equals(userCode) && StringUtils.isEmpty(adminRoleCode)) {
                logger.warn("Route=>UserInfoController method=>userInfoImport 用户数据导入，当前登录人角色信息异常");
                return Result.failWithMsg("登录人角色信息异常");
            }
            ExcelUtil<UserInfoExcelDto> excelUtil = new ExcelUtil<UserInfoExcelDto>(UserInfoExcelDto.class);
            List<UserInfoExcelDto> userInfoExcelDtos = excelUtil.importExcel(file.getInputStream());
            if (org.apache.commons.collections.CollectionUtils.isEmpty(userInfoExcelDtos)) {
                logger.info("用户数据导入失败","文件解析为空");
                return Result.fail(Result.FAIL, "文件解析不可为空");
            }
            //用户数据导入
            Result msg = userInfoService.userInfoImport(userInfoExcelDtos, companyCode, userName, adminRoleCode);
            return msg;
        } catch (RuntimeException e) {
            logger.info("Route=>UserInfoController method=>userInfoImport 用户数据导入失败：{}", file.getName());
            return handleException(e);
        }
    }

    /**
     * 用户数据导出
     * @param userInfoSelectDto
     * @param response
     * @return
     */
    @PostMapping("/userInfoExport")
    public void userInfoExport(@RequestBody UserInfoSelectDto userInfoSelectDto, HttpServletResponse response) {
        logger.info("Route=>UserInfoController method=>userInfoExport 用户数据导出：{}", Json.toJsonString(userInfoSelectDto));
        try {
            String companyCode = response.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>selectUserInfo 根据条件查询用户数据，当前登录人归属租户信息异常");
                return ;
            }
            userInfoSelectDto.setCompanyCode(companyCode);

            if (StringUtils.isEmpty(userInfoSelectDto.getOrgTypeCode())) {
                logger.info("用户数据导出失败,参数非法{}", userInfoSelectDto.getOrgTypeCode());
                return;
            }
            //判断是否为只是查询组织类型下的所有人员
            if (userInfoSelectDto.getOrgLevel() == null && userInfoSelectDto.getOrgLevel() != 0) {
                if (StringUtils.isEmpty(userInfoSelectDto.getOrgCode())) {
                    logger.info("根据条件查询用户数据失败,参数非法{}", userInfoSelectDto.getOrgCode());
                    return;
                }else {
                    //判断用户组织编码后是否有","相连
                    if (!",".equals(userInfoSelectDto.getOrgCode().substring(userInfoSelectDto.getOrgCode().length() - 1))) {
                        userInfoSelectDto.setOrgCode(userInfoSelectDto.getOrgCode() + ",");
                    }
                }
            }
            if (StringUtils.isNotEmpty(userInfoSelectDto.getEmployeeCode())) {
                if (userInfoSelectDto.getEmployeeCode().length() > 30) {
                    logger.info("用户数据导出失败,参数非法{}", userInfoSelectDto.getEmployeeCode());
                    return;
                }
            }
            if (StringUtils.isNotEmpty(userInfoSelectDto.getName())) {
                if (userInfoSelectDto.getName().length() > 30) {
                    logger.info("用户数据导出失败,参数非法{}", userInfoSelectDto.getName());
                    return;
                }
            }
            if (StringUtils.isNotEmpty(userInfoSelectDto.getPhone())) {
                if (userInfoSelectDto.getPhone().length() > 20) {
                    logger.info("用户数据导出失败,参数非法{}", userInfoSelectDto.getPhone());
                    return;
                }
            }
            //根据条件查询用户数据
            List<UserInfoSelectVo> list = userInfoService.selectUserData(userInfoSelectDto);
            if (list.size() > 5000) {
                logger.info("用户数据导出失败,参数非法{}", list.size());
                return;
            }
            if (CollectionUtils.isEmpty(list)) {
                logger.info("用户数据导出失败,参数非法{}", list.size());
                return;
            }
            ExcelUtil<UserInfoSelectVo> util = new ExcelUtil<UserInfoSelectVo>(UserInfoSelectVo.class);
            util.exportExcel(response, list, "用户数据", "用户导出数据","用户数据导出文件.xlsx");
            logger.info("Route=>UserInfoController method=>userInfoExport 用户数据导出成功：{}", list.size());
        } catch (RuntimeException | UnsupportedEncodingException e) {
            logger.info("Route=>UserInfoController method=>userInfoExport 用户数据导出失败：{}", Json.toJsonString(userInfoSelectDto), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取用户的账号数据
     * @return
     */
    @PostMapping("/selectAccountData")
    public Result selectAccountData(HttpServletRequest httpServletRequest,@RequestBody UserAccountSelectDto accountSelectDto) {
        logger.info("Route=>UserInfoController method=>selectAccountData 获取用户的账号数据：{}", Json.toJsonString(accountSelectDto));
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>selectAccountData 获取用户的账号数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }

            if (StringUtils.isEmpty(accountSelectDto.getUserCode())) {
                logger.info("获取用户的账号数据失败,参数非法{}", accountSelectDto.getUserCode());
                return Result.fail(Result.FAIL, "请选择则数据！");
            }
            IdentityAccountDto identityAccountDto = new IdentityAccountDto();
            identityAccountDto.setUserCode(accountSelectDto.getUserCode());
            identityAccountDto.setAccountId(accountSelectDto.getAccountId());
            identityAccountDto.setCompanyCode(companyCode);

            logger.info("Route=>远程调用ident  feign=>queryAccountList获取用户的账号数据前：{}", identityAccountDto);
            Result result = identFeignInterface.queryAccountList(identityAccountDto);
            if (result.isSuccess()) {
                Object data = result.getData();
                logger.info("Route=>远程调用ident  feign=>queryAccountList获取用户的账号数据成功后：{}", Json.toJsonString(data));
                return Result.success("成功", data);
            } else {
                logger.info("Route=>远程调用ident  feign=>queryAccountList获取用户的账号数据失败后：{}", Json.toJsonString(result));
                return Result.success("获取用户的账号数据失败");
            }
        } catch (RuntimeException e) {
            logger.info("Route=>UserInfoController method=>selectAccountData 获取用户的账号数据失败：{}", Json.toJsonString(accountSelectDto), e);
            return handleException(e);
        }
    }

    /**
     * 查询所有用户类型数据
     * @return
     */
    @PostMapping("/getUserType")
    public Result getUserType(HttpServletRequest httpServletRequest) {
        logger.info("Route=>UserInfoController method=>getUserType 查询所有用户类型数据：{}", "");
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>getUserType 查询所有用户类型数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            List<IamUserTypeBase> userTypeBaseList = userInfoService.getUserType(companyCode);
            return Result.success("查询所有用户类型数据成功", userTypeBaseList);
        } catch (RuntimeException e) {
            logger.info("Route=>UserInfoController method=>getUserType 查询所有用户类型数据失败：{}", "", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：根据 手机+组织类型编码+租户编码 查询是否存在该用户
     * @param iamUserInfoParam
     * @return Result
     */
    @PostMapping("/getUserInfoByAny")
    public Result getUserInfoByAny(@RequestBody IamUserInfoParam iamUserInfoParam) {
        try {
            if(StringUtils.isBlank(iamUserInfoParam.getName()) || StringUtils.isBlank(iamUserInfoParam.getPhone())){
                return Result.fail("入参的姓名和手机不能为空！",null);
            }
            IamUserInfo iamUserInfo = userInfoService.getUserInfoByAny(iamUserInfoParam);
            if(Objects.isNull(iamUserInfo)){
                return Result.fail("查不到任何用户信息！",null);
            }
            return Result.success("查询用户信息成功！",iamUserInfo);
        }catch (Exception e){
            logger.info("Route=>UserInfoController method=>getUserInfoByAny 根据姓名或手机查询是否存在该用户异常：{}，入参：{}", e.getMessage(),Json.toJsonString(iamUserInfoParam));
            return handleException(e,e.getMessage());
        }
    }

    /**
     * 功能描述：角色关联用户查询-——查询某个角色下绑定的用户
     * @param iamRoleQueryUserParam
     * @return
     */
    @PostMapping("/queryRoleBindUsers")
    public Result<PageInfo<IamUserInfo>> roleBindUsers(@RequestBody IamRoleQueryUserParam iamRoleQueryUserParam) {
        try {
            logger.info("Route==>UserInfoApiController method=queryRoleBindUsers,入参userCode为:{}", JSON.toJSONString(iamRoleQueryUserParam));
            PageInfo<IamUserInfo> pageInfo = userInfoService.queryRoleBindUsers(iamRoleQueryUserParam);
            if(CollectionUtils.isEmpty(pageInfo.getList())){
                return Result.failWithMsg("根据usercode查询不到任何用户信息！");
            }
            return Result.success("根据usercode查询用户信息成功！",pageInfo);
        }catch (Exception e){
            logger.error("Route==>UserInfoApiController method=queryRoleBindUsers 根据用户id查询用户信息异常：{},入参userCode为:{}",e.getMessage(), JSON.toJSONString(iamRoleQueryUserParam));
            return handleException(e,e.getMessage());
        }
    }
    /**
     * 功能描述：查询某个角色下还未绑定的用户
     * @param iamRoleQueryUserParam
     * @return Result
     */
    @PostMapping("/queryRoleNotBindUsers")
    public Result queryRoleNotBindUsers(@RequestBody IamRoleQueryUserParam iamRoleQueryUserParam) {
        try {
            if(CollectionUtils.isEmpty(iamRoleQueryUserParam.getUserCodes())){
                logger.warn("入参usercode为空！");
//                return Result.fail("入参usercode为空！",null);
            }
            PageInfo<IamUserInfo> pageInfo = userInfoService.queryRoleNotBindUsers(iamRoleQueryUserParam);
            if(CollectionUtils.isEmpty(pageInfo.getList())){
                return Result.success("该角色下没有可以绑定的用户！",null);
            }
            return Result.success("查询某角色下未绑定的用户成功！",pageInfo);
        }catch (Exception e){
            logger.info("Route=>UserInfoController method=>roleNotBindUsers 查询某角色下未绑定的用户异常：{}，入参：{}", e.getMessage(),Json.toJsonString(iamRoleQueryUserParam));
            return handleException(e,e.getMessage());
        }
    }

    /**
     * 功能描述：根据用户查询用户详情数据
     * @param userCode
     * @return
     */
    @PostMapping("/queryByUserCode")
    public Result queryDetailByUserCode(@RequestParam("companyCode") String companyCode, @RequestParam("userCode") String userCode) {
        logger.info("Route=>UserInfoController method=>queryDetailByUserCode 查询用户详情数据：{}", userCode);
        try {
            if (StringUtils.isEmpty(companyCode)) {
                return Result.failWithMsg("归属租户为空");
            }
            if (StringUtils.isEmpty(userCode)) {
                return Result.failWithMsg("用户编码为空");
            }
            //查询用户详情数据
            UserInfoVo userInfoVo = userInfoService.selectUserDetail(userCode, companyCode);
            return new Result(Result.SUCCESS, "查询用户详情数据成功", userInfoVo);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>queryDetailByUserCode 查询用户详情数据异常，入参：{}，异常：{}", userCode, e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：根据用户编码（usercode工号）,开通账号
     * @param userCode
     * @return
     */
    @PostMapping("/openAccount")
    public Result openAccount(@RequestBody List<String> userCode) {
        if(CollectionUtils.isEmpty(userCode)){
            return  Result.failWithMsg("请至少勾选一条用户信息进行账户开通！");
        }
        logger.info("Route==>UserInfoApiController method=openAccount需要开通账户的用户编码为:{}", JSON.toJSONString(userCode));
        try {
            boolean openAccount = userInfoService.openAccount(userCode);
            if(openAccount){
                logger.info("Route==>UserInfoController method=openAccoun 用户开通账户成功:{}",JSON.toJSONString(userCode));
                return Result.success("根据用户编码开通账号成功！",JSON.toJSONString(userCode));
            }
            return  Result.failWithMsg("根据用户编码开通账号失败！");
        }catch (Exception e){
            logger.error("Route==>UserInfoController method=openAccount 根据用户编码开通账号异常{}：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：根据用户id（usercode）,批量将用户设置为未开通
     * @param userCode
     * @return Result
     */
    @PostMapping("/batchDeleteByUserCode")
    public Result batchDeleteByUserCode(@RequestBody List<String> userCode) {
        if(CollectionUtils.isEmpty(userCode)){
            return  Result.failWithMsg("前台根据用户编码修改用户是否开通标识的参数为null");
        }
        try {
            int count = userInfoService.batchDeleteByUserCode(userCode);
            if( 0==count){
                return Result.failWithMsg("批量将用户设置为未开通,失败！");
            }
            logger.info("Route==>UserInfoController method=batchDeleteByUserCode 批量将{}条用户设置为未开通成功!",count);
            return Result.success("批量将用户设置为未开通,成功！{}",JSON.toJSONString(userCode));
        }catch (Exception e){
            logger.error("Route==>UserInfoController method=batchDeleteByUserCode 批量将用户设置为未开通异常：{}",e.getMessage());
            return handleException(e,e.getMessage());
        }
    }

    /**
     * 根据用户信息查询应用详情数据  远程调用  ident
     * @param httpServletRequest
     * @param iamApplyDetaislParam
     * @return
     */
    @PostMapping("/getApplyDetails")
    public Result getApplyDetails(HttpServletRequest httpServletRequest,@RequestBody IamApplyDetaislParam iamApplyDetaislParam) {
        logger.info("Route=>UserInfoController method=>getApplyDetails 根据用户信息查询应用详情数据：{}", Json.toJsonString(iamApplyDetaislParam));
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                logger.warn("Route=>UserInfoController method=>getApplyDetails 根据用户信息查询应用详情数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            iamApplyDetaislParam.setCompanyCode(companyCode);

            logger.info("Route=>远程调用ident  feign=>getApplicationDetails 根据用户信息查询应用详情数据：{}", Json.toJsonString(iamApplyDetaislParam));
            Result result = identFeignInterface.getApplicationDetails(iamApplyDetaislParam);
            if (result.isSuccess()) {
                Object data = result.getData();
                logger.info("Route=>远程调用ident  feign=>getApplicationDetails 根据用户信息查询应用详情数据,成功后：{}", Json.toJsonString(data));
                return Result.success("根据用户信息查询应用详情数据成功", data);
            } else {
                logger.info("Route=>远程调用ident  feign=>getApplicationDetails 根据用户信息查询应用详情数据,失败后：{}", Json.toJsonString(result));
                return Result.failWithMsg("根据用户信息查询应用详情数据");
            }
        } catch (RuntimeException e) {
            logger.info("Route=>UserInfoController method=>getApplyDetails 根据用户信息查询应用详情数据,失败：{}", Json.toJsonString(iamApplyDetaislParam), e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：查询动态用户组下的用户（分页展示）
     * @param：iamFilterRulesParam 筛选规则条件
     * @return Result
     */
    @PostMapping("/queryDynamicUserGroupUserInfo")
    public Result queryDynamicUserGroupUserInfo(HttpServletRequest request, @RequestBody IamFilterRulesParam iamFilterRulesParam) {
        try {
            String companyCode = (String) request.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            iamFilterRulesParam.setCompanyCode(companyCode);
            logger.info("Route==>UserInfoController method=>queryDynamicUserGroupUserInfo 查询动态用户组下的用户入参：{}",Json.toJsonString(iamFilterRulesParam));
            PageInfo<IamUserInfo> iamUserInfoPageInfo = userInfoService.queryDynamicUserGroupUserInfo(iamFilterRulesParam);
            if(0==iamUserInfoPageInfo.getTotal()){
                return Result.successWithMsg("该动态用户组下无任何用户！");
            }
            return Result.success("根据动态用户组筛选规则查询用户成功！",iamUserInfoPageInfo);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>queryDynamicUserGroupUserInfo 查询动态用户组下的用户异常: {}，入参：{}", e.getMessage(),Json.toJsonString(iamFilterRulesParam));
            return handleException(e,e.getMessage());
        }
    }

    /**
     * 功能描述：账户中心——账号列表分页条件查询（普管）
     * @param iamQueryUserByOrgParam
     * @return PageInfo
     */
    @PostMapping(value = "/queryAccountListRange")
    Result<IamUserInfo> queryAccountListRange(@RequestBody IamQueryUserByOrgParam iamQueryUserByOrgParam){
        try {
            logger.info("Route==>UserInfoController method=>queryAccountListRange 账号列表分页条件查询（普管）入参：{}",Json.toJsonString(iamQueryUserByOrgParam));
            List<String> iamUserInfoList = userInfoService.queryAccountListRange(iamQueryUserByOrgParam);
            if(CollectionUtils.isEmpty(iamUserInfoList)){
                return Result.success("无数据！",null);
            }
            return Result.success("账号列表分页条件查询（普管）,查询用户成功！",iamUserInfoList);
        } catch (Exception e) {
            logger.error("Route==>UserInfoController method=>queryAccountListRange 账号列表分页条件查询（普管）异常: {}，入参：{}", e.getMessage(),Json.toJsonString(iamQueryUserByOrgParam));
            return handleException(e,e.getMessage());
        }
    }

}
